//
//  YCNormalActionSheet.m
//  DemoActionSheet
//
//  Created by tangkexiong on 16/3/16.
//  Copyright © 2016年 tangkexiong. All rights reserved.
//

#import "YCNormalActionSheet.h"

#pragma mark - YCNormalActionSheetCell

@implementation YCNormalActionSheetCell



@end

#pragma mark - YCNormalActionSheet

#define kDefaultMaxNum 4

@interface YCNormalActionSheet ()<UITableViewDataSource, UITableViewDelegate> {
    NSString *_title;
    NSArray *_actions;
    
    UIWindow *_window;
    UIWindow *_oldWindow;
}

@end

@implementation YCNormalActionSheet

#pragma mark - UIViewController

+ (instancetype)instanceWithTitle:(NSString *)title delegate:(id<YCNormalActionSheetDelegate>)delegate actions:(NSString *)action, ... {
    YCNormalActionSheet *sheet = [[YCNormalActionSheet alloc] initWithNibName:@"YCNormalActionSheet" bundle:nil];
    sheet.myDelegate= delegate;
    
    NSMutableArray *array = [[NSMutableArray alloc] init];
    va_list list;
    va_start(list, action);
    if (action) {
        [array addObject:action];
        NSString *str;
        while ((str=va_arg(list, NSString *))) {
            [array addObject:str];
        }
    }
    va_end(list);
    
    [sheet setTitle:title];
    [sheet setActions:array];
    
    return sheet;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self _configView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)_configView {
    self.view.backgroundColor = [UIColor clearColor];
    
    _bgView.alpha = 0.f;
    
    _cancelBtn.layer.cornerRadius = 5.f;
    _cancelBtn.clipsToBounds = YES;
    [_cancelBtn setTitleColor:_tintColor forState:UIControlStateNormal];
    
    _sheetView.layer.cornerRadius = 5.f;
    _sheetView.clipsToBounds = YES;
    
    _titleLabel.text = _title;
    
    [_myTableView registerNib:[UINib nibWithNibName:@"YCNormalActionSheetCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    _myTableView.dataSource = self;
    _myTableView.delegate = self;
    
    if (!_title) {
        _constraintTitleViewHeight.constant = 0.f;
    }
    
    if (_actions.count > kDefaultMaxNum) {
        _constraintActionSheetHeight.constant = _constraintTitleViewHeight.constant + 44.f + 8.f + 8.f + kDefaultMaxNum * 44.f + 22.f;
    } else {
        _constraintActionSheetHeight.constant = _constraintTitleViewHeight.constant + 44.f + 8.f + 8.f + _actions.count * 44.f;
    }
    _constraintActionSheetBottom.constant = -_constraintActionSheetHeight.constant;
    
    [self.view layoutIfNeeded];
}

#pragma mark - Setter and Getter

- (void)setTitle:(NSString *)title {
    _title = [title copy];
}

- (void)setActions:(NSArray *)array {
    _actions = array;
}

#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _actions.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YCNormalActionSheetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.nameLabel.text = _actions[indexPath.row];
    cell.nameLabel.textColor = self.tintColor;
    
    cell.lineViews.hidden = indexPath.row == _actions.count-1;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self dismiss];
    
    if ([_myDelegate respondsToSelector:@selector(normalActionSheet:didSelectAtIndex:)]) {
        [_myDelegate normalActionSheet:self didSelectAtIndex:indexPath.row];
    }
}

#pragma mark - Control

- (void)show {
    _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _window.windowLevel = 440.f;
    _window.rootViewController = self;
    _oldWindow = [UIApplication sharedApplication].keyWindow;
    [_window makeKeyAndVisible];
    
    [self _showAnimation];
}

- (void)_showAnimation {
    _constraintActionSheetBottom.constant = 0.f;
    _bgView.alpha = 0.f;
    [UIView animateWithDuration:0.3f animations:^{
        _bgView.alpha = 0.5f;
        
        [self.view layoutIfNeeded];
    }];
}

- (void)dismiss {
    [self _dismissAnimation];
}

- (void)_dismissAnimation {
    _constraintActionSheetBottom.constant = -_constraintActionSheetHeight.constant;
    _bgView.alpha = 0.5f;
    [UIView animateWithDuration:0.3f animations:^{
        _bgView.alpha = 0.f;
        
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self.view removeFromSuperview];
        
        _window = nil;
        
        [_oldWindow makeKeyAndVisible];
    }];
}

#pragma mark - IBAction

- (IBAction)cancelBtnClicked:(id)sender {
    [self dismiss];
    
    if ([_myDelegate respondsToSelector:@selector(normalActionSheetDidCancel:)]) {
        [_myDelegate normalActionSheetDidCancel:self];
    }
}

@end
